using UnityEngine;
using System.IO;
public class PlayerDataManager : MonoBehaviour
{
	public Transform player;
	private string savePath;
	private void Awake()
	{
		//获取存档文件路径
		savePath = Path.Combine(Application.persistentDataPath, "saveData.json");
        Debug.Log(savePath);
	}
	private void Update(){
		if (Input.GetKeyDown(KeyCode.O)) Save();//当按下'o'键时保存玩家数据
		if (Input.GetKeyDown(KeyCode.P)) Load();//当按下'P'键时加载玩家数据
	}
	public void Save(){
		PlayerGameData saveData = new PlayerGameData();
		saveData.playerPosition = player.position;
		string jsonData = JsonUtility.ToJson(saveData);
		File.WriteAllText(savePath, jsonData);
		Debug.Log("Save ok!");
	}
	public void Load(){
		//首先对路径做一个安全性检查
		if(File.Exists(savePath)){
			string jsonData = File.ReadAllText(savePath);
			//将JSON数据反序列化为游戏数据对象
			PlayerGameData loadedData = JsonUtility.FromJson<PlayerGameData>(jsonData);
			player.position = loadedData.playerPosition;
			Debug.Log("Load ok!");
		}else{
			Debug.Log("Load ok!");
		}
	}
}

public class PlayerGameData
{
    public Vector3 playerPosition;
    // 可以添加其他的游戏数据字段
}